/*
 * @Author: tcosfish
 * @Date: 2022-07-22 10:45:48
 * @LastEditors: tcosfish
 * @LastEditTime: 2022-07-22 16:54:24
 * @FilePath: \loadFile\src\features\month.create.ts
 */

import exceljs from "exceljs"

import * as path from "path"

import { transXLS } from "@/utils/transXLSForXLSX.util"

export async function copy_sheet(target_file: string) {
  // 1. 判断被赋值文件是否为 xls 类型, 是则进行转换
  const ext = target_file.substring(target_file.lastIndexOf(".") + 1)
  if (ext === "xls") {
    target_file = await transXLS(target_file)
    console.log("它的新路径为: ", target_file)
  }

  // 2. 对 xlsx 文件进行操作
  const workbook = new exceljs.Workbook()
  await workbook.xlsx.readFile(target_file)

  const sheet_1 = workbook.getWorksheet(1)
  sheet_1.properties.defaultRowHeight = 20
  sheet_1.properties.defaultColWidth = 30

  let row_1 = sheet_1.getRow(1)
  row_1.hidden = true
  row_1.commit()

  sheet_1.mergeCells("A2:F2")
  let row_2 = sheet_1.getRow(2)
  row_2.font = { size: 22 }
  row_2.height = 28.5
  row_2.alignment = { vertical: "middle", horizontal: "center" }
  row_2.commit()

  sheet_1.eachRow((row, row_number) => {
    if (row_number >= 4 && row_number < sheet_1.rowCount) {
      row.eachCell((cell, cell_number) => {
        if (cell_number < sheet_1.columnCount) {
          cell.border = {
            top: { style: "thin" },
            left: { style: "thin" },
            bottom: { style: "thin" },
          }
        } else if (cell_number === sheet_1.columnCount) {
          cell.border = {
            top: { style: "thin" },
            left: { style: "thin" },
            bottom: { style: "thin" },
            right: { style: "thin" },
          }
        }
      })
    }
    row.commit()
  })

  let row_5 = sheet_1.getRow(5)
  row_5.eachCell((cell, cell_num) => {
    console.log(`${cell_num}: ${cell}`)
  })

  await workbook.xlsx.writeFile(target_file)
}
